package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.dao.StudentClassDao;
import com.example.domain.StudentClass;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.StudentClassService;
import com.example.tool.DateTool;

@Service
public class StudentClassServiceImpl implements StudentClassService {

    @Autowired
    StudentClassDao studentClassDao;

    /**
     * @describe 添加学生班级信息
     */
    @Override
    public Result addStudentClass(String json) {
        JSONObject parseObject = JSON.parseObject(json);
        String[] classId = parseObject.getString("classId").split(",");
        String[] userId = parseObject.getString("userId").split(",");
        List<StudentClass> list = new ArrayList<StudentClass>();

        if (userId != null && classId != null) {
            for (int j = 0; j < userId.length; j++) {
                StudentClass studentClass = new StudentClass();
                studentClass.setClassId(Integer.parseInt(classId[0]));
                studentClass.setUserId(Integer.parseInt(userId[j]));
                // 获取系统当前时间作为记录创建时间
                studentClass.setRecordTime(DateTool.getTimestamp());

                list.add(studentClass);
            }

        }
        // 调用JPA保存实体方法
        List<StudentClass> studentClass1 = studentClassDao.saveAll(list);

        if (studentClass1 != null) {
            return ResultFactory.buildSuccessResult(studentClass1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新学生班级信息
     */
    @Override
    public Result updateStudentClass(StudentClass studentClass) {
        if (studentClass != null) {
            // 获取系统当前时间作为记录更新时间
            studentClass.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        StudentClass studentClass1 = studentClassDao.saveAndFlush(studentClass);

        if (studentClass1 != null) {
            return ResultFactory.buildSuccessResult(studentClass1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学生班级编号删除学生班级信息
     */
    @Override
    public Result deleteStudentClassById(String studentClassId) {
        int row = studentClassDao.deleteByStudentClassId(Integer.parseInt(studentClassId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学生班级信息
     */
    @Override
    public Result queryAllStudentClass() {
        List<StudentClass> list = studentClassDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询学生班级信息
     */
    @Override
    public Result queryStudentClassById(String studentClassId) {
        List<StudentClass> list = studentClassDao.findByStudentClassId(Integer.parseInt(studentClassId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加学生班级信息
     */
    @Override
    public Result addStudentClassByExcel(List<StudentClass> list) {
        List<StudentClass> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = studentClassDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学生班级编号批量删除学生班级信息
     */
    @Override
    public Result deleteStudentClassByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = studentClassDao.deleteByStudentClassIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 学生班级信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;

        if (info[0].equals("className")) {

            list = studentClassDao.findByClassNameLike(info[1]);

        } else if (info[0].equals("userName")) {

            list = studentClassDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = studentClassDao.findByNickNameLike(info[1]);
        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有未分配班级的学生
     */
    @Override
    public Result queryAllStudents() {

        List<Map<String, Object>> list = studentClassDao.findAllStudents();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 查询学生已经分配班级的班级名称
     */
    @Override
    public Result queryAllClass() {

        List<Map<String, Object>> list = studentClassDao.findAllClass();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 查询所有学生和班级的已分配信息
     */
    @Override
    public Result queryAllStudentAndClassInfo() {
        List<Map<String, Object>> list = studentClassDao.queryAllStudentAndClassInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有班级名称
     */
    @Override
    public Result queryAllClassInfo() {
        List<Map<String, Object>> list = studentClassDao.queryAllClassInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
